<template>
	<view class="add page bgf7 p12">
		<view class="bgff p22">

			<input v-model="title" class="flex bor ml8 r40 px12 py6" placeholder="请输入标题" />


			<view v-for="(item,index) in list" :key="index">
				<input v-model="item.content" class=" bor ml8 r40 px12 py6 mt10" placeholder="请输入内容" />

			</view>
			<view class="flex flexC aliC mt36">
				<view class="mr30 flex1 c00 bgf6 r40 py12" @click="addItem">添加数据项</view>
				<view class="flex1 bg41 r40 py12" @click="save">保存数据</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {

				selectShop: null,
				list: [],
				title: ''
			};
		},
		onLoad(option) {
			var shop = option.selectShop;
			if (shop != '') {
				this.selectShop = JSON.parse(shop);
			}

			this.getNotice();
		},

		methods: {


			getNotice() {
				let param = {
					shop_id: this.selectShop.id,

				};
				let that = this;
				this.$api.getNotice(param).then(res => {

					if (res.code == 1) {
						//this.list = res.data;
						if(res.data != null){
							this.title = res.data.title;
							this.list = res.data.content;
						}else{
							this.addItem();
						}

					} else {

					}
				})
			},



			addItem() {

				var obj = new Object();
				obj.content = "";
				this.list.push(obj);
			},

			save() {
				var isOk = true;
				for (var i = 0; i < this.list.length; i++) {

					if (this.list[i].content == "") {
						isOk = false;
					}
				}

				if (this.title == "") {
					uni.showToast({
						title: "标题不能为空",
						icon: 'none'
					})
					return;
				}
			

				let param = {
					id: this.selectShop.id,
					content: JSON.stringify(this.list),
					title:this.title
				};
				let that = this;

				this.$api.addNotice(param).then(res => {

					if (res.code == 1) {
						uni.showModal({
							title: "添加提示",
							content: "添加成功",
							showCancel: false,
							complete() {
								uni.navigateBack({
									delta: 1
								})
							}
						})

					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})

			}
		}

	}
</script>

<style lang="scss">
	.w45 {
		width: 60rpx;
		height: 60rpx;
	}
	.w100{
		width: 100%;
	}
</style>